package com.ht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.data.Result;
import com.ht.domain.po.TbSchedule;
import com.ht.em.ResultEnum;
import com.ht.mapper.ScheduleMapper;
import com.ht.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    /**
     * 添加医生排班
     * @param doctorId
     * @param schedule
     * @return
     */
    @Override
    public int addSchedule(Long doctorId, TbSchedule schedule) {
        List<TbSchedule> scheduleList = scheduleMapper.selectList(new QueryWrapper<TbSchedule>()
                .eq("doctor_id", doctorId)
                .eq("date_status", schedule.getDateStatus())
                .eq("work_date", schedule.getWorkDate()));
        if (scheduleList.size() > 0){
            return 2;
        }
        schedule.setDoctorId(doctorId);
        return scheduleMapper.insert(schedule);
    }

    /**
     * 删除医生排班
     * @param doctorId
     * @param dateStatus
     * @param workDate
     * @return
     */
    @Override
    public Result deleteScheduleByDoctorId(Long doctorId, Integer dateStatus, String workDate) {
        List<TbSchedule> scheduleList = scheduleMapper.selectList(new QueryWrapper<TbSchedule>()
                                                                .eq("doctor_id", doctorId)
                                                                .eq("date_status", dateStatus)
                                                                .eq("work_date", workDate));
        if (scheduleList.size() <= 0){
            return Result.response(ResultEnum.NO_SCHEDULE);
        }
        int i = scheduleMapper.delete(new QueryWrapper<TbSchedule>().eq("doctor_id", doctorId)
                .eq("date_status", dateStatus).eq("work_date", workDate));
        if (i <= 0){
            return Result.ERROR();
        }
        return Result.OK();
    }
}
